package com.wechat.wechat.controller;

import com.wechat.wechat.config.ProjectUrlConfig;
import com.wechat.wechat.constant.CookieConstant;
import com.wechat.wechat.constant.RedisConstant;
import com.wechat.wechat.entity.SellerInfo;
import com.wechat.wechat.enums.ResultEnum;
import com.wechat.wechat.service.SellerService;
import com.wechat.wechat.service.impl.SellerServiceImpl;
import com.wechat.wechat.utils.CookieUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author NN
 * @2020/2/28 20:32
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {
    @Autowired
    private SellerService sellerService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    /**
     * 登陆
     */
    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              Map<String,Object> map,
                              HttpServletResponse response)
    {
        //1.openid和数据库里的数据匹配
        SellerInfo sellerInfo=sellerService.findByOpenid(openid);
        if(sellerInfo == null)
        {
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        //2.设置token至redis
        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPIRE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),
                openid, expire, TimeUnit.SECONDS);//Token的key,token的value，有效期，有效期的时间单位
        //3.设置token至cookie
        CookieUtil.set(response, CookieConstant.TOKEN,token,expire);
        return new ModelAndView("redirect:"+projectUrlConfig.getSell()
                                +"/sell/seller/order/list");
    }

    /**
     * 登出
     */
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                               Map<String,Object> map,
                               HttpServletResponse response){
        //1.查询cookie
        Cookie cookie=CookieUtil.get(request,CookieConstant.TOKEN);
        if(cookie!=null)
        {
            //2.清除redis
            redisTemplate.opsForValue().getOperations().delete(
                    String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
            //3.清除cookie
            CookieUtil.set(response,CookieConstant.TOKEN,null,0);
        }
        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);
    }


}
